import java.math.BigInteger;

/**
 * @author by zyl
 * @date 2020/3/28.
 */
public class Agreement {
    private static ECPoint P;
    private static ECPoint PK;
    private static ECCOperator ec;

    static {
        ec=new ECCOperator();
        ECPoint G=new ECPoint(new BigInteger("2"),new BigInteger("4"));
        P=ec.multiply(G,new BigInteger("123456"));
        String sPK= FileUtil.readTag(CommonValue.KeyTable,"KGC").split(" ")[1];
        PK=new ECPoint(new BigInteger(sPK.split(",")[0]),new BigInteger(sPK.split(",")[1]));
    }

    public static void agreement(){
        String fromA=SocketUtils.in(CommonValue.A_B_Port);
        System.out.println("Receiving fromA:"+fromA);
        String Ea=fromA.split(":")[0];
        String RA=fromA.split(":")[1];
        String IDB=FileUtil.readTag(CommonValue.IDTable,"B1").split(" ")[1];
        String KBi=FileUtil.readTag(CommonValue.KeyTable,"B1").split(" ")[1];
        String HBA=HashUtils.Hash(RA+KBi+IDB);
        System.out.println("Computing HBA:"+HBA);
        String Ea_=Base64.decode(Ea);
        String linkData1=XorUtils.xor(Ea_,HBA);
        String Xa=linkData1.split(":")[0];
        System.out.println("Decoding Xa:"+Xa);
        String uIDA=linkData1.split(":")[1];
        System.out.println("Decoding uIDA:"+uIDA);
        String KCij=linkData1.split(":")[2];
        System.out.println("Decoding KCij:"+KCij);
        String uIDCij=linkData1.split(":")[3];
        System.out.println("Decoding uIDCij:"+uIDCij);
        String xRA=linkData1.split(":")[4];
        System.out.println("Decoding RA:"+xRA);
        String IDA=FileUtil.readTag(CommonValue.IDTable,"A").split(" ")[1];
        if(IDA.equals(uIDA)){
            System.out.println("B verifies A success!");
        }
        else{
            System.out.println("B verifies A failed!");
        }
        String request="request";
        SocketUtils.out(CommonValue.B_C_IP,CommonValue.B_C_Port,request);
        String fromC=SocketUtils.in(CommonValue.C_B_Port);
        System.out.println("Receiving fromC:"+fromC);
        String Ec=fromC.split(":")[0];
        String RC=fromC.split(":")[1];
        String HBC=HashUtils.Hash(RC+KCij+uIDCij);
        System.out.println("Computing HBC:"+HBC);
        String Ec_=Base64.decode(Ec);
        String linkData2=XorUtils.xor(Ec_,HBC);
        String Xc=linkData2.split(":")[0];
        System.out.println("Decoding Xc:"+Xc);
        String uIDC=linkData2.split(":")[1];
        System.out.println("Decoding uIDC:"+uIDC);
        String xRC=linkData2.split(":")[2];
        System.out.println("Decoding RC:"+xRC);
        if(uIDC.equals(uIDCij)){
            System.out.println("B verifies C success!");
        }
        else{
            System.out.println("B verifies C failed!");
        }
        String RB=CharacterUtils.getRandomInteger(16);
        System.out.println("Selects a Random RB");
        String VB=HashUtils.Hash(uIDA+IDB+uIDC+RB);
        System.out.println("Computing VB:"+VB);
        String linkData3=uIDA+":"+IDB+":"+uIDC+":"+Xa+":"+Xc+":"+VB;
        String Ebc=Base64.encode(XorUtils.xor(linkData3,HBC));
        String toC=Ebc+":"+RB;
        System.out.println("toC:"+toC);
        SocketUtils.out(CommonValue.B_C_IP,CommonValue.B_C_Port,toC);
        String Eba=Base64.encode(XorUtils.xor(linkData3,HBA));
        String toA=Eba+":"+RB;
        System.out.println("toA:"+toA);
        SocketUtils.out(CommonValue.B_A_IP,CommonValue.B_A_Port,toA);

    }

    public static void main(String args[]){
        agreement();
    }

}